package com.t4.courseware.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.t4.courseware.common.Result;
import com.t4.courseware.handler.NoAuth;
import com.t4.courseware.model.WXAuth;
import com.t4.courseware.pojo.dto.UserDto;
import com.t4.courseware.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @NoAuth
    @GetMapping("/getSessionId")
    public Result getSessionId(@RequestParam String code) {
        String sessionId = userService.getSessionId(code);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("sessionId", sessionId);
        return Result.SUCCESS(hashMap);
    }

    @NoAuth
    @PostMapping("/authLogin")
    public Result authLogin(@RequestBody WXAuth wxAuth) {
        return userService.authLogin(wxAuth);
    }

    @GetMapping("/userinfo")
    public Result userinfo(Boolean refresh){
        return userService.userinfo(refresh);
    }


    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserDto userDto){
        return userService.updateUser(userDto);
    }

    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile multipartFile){
        return userService.uploadAvatar(multipartFile);
    }
}